package com.hx.er.action.admin.rest;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.hx.er.action.base.ErAdminAction;
import com.hx.er.bean.base.LanguageBean;
import com.hx.er.bean.base.RestraurantBean;
import com.hx.er.bean.base.UserBean;
import com.hx.er.common.system.Constants;
import com.hx.er.common.system.ErLanguages;
import com.hx.er.common.system.ErProperties;
import com.hx.er.common.util.CharUtil;
import com.hx.er.common.util.MD5Util;
import com.hx.er.common.util.StringUtil;
import com.hx.er.service.admin.IAdminRestService;

public class AdminRestAddAction extends ErAdminAction {
	// 序列化ID
	private static final long serialVersionUID = -1127474451530316813L;

	// 页面消息
	public static final String SERVER_NAME_UNIQUE_MSG = "admin.restList.message.add.unique.serverName";
	public static final String SERVER_NAME_EMPTY_MSG = "admin.restList.message.add.empty.serverName";
	public static final String REST_NAME_EMPTY_MSG = "admin.restList.message.add.empty.restName";
	public static final String EXP_DATE_EMPTY_MSG = "admin.restList.message.add.empty.expirationDate";
	public static final String EXP_DATE_EXPR_MSG = "admin.restList.message.add.expr.expirationDate";

	public static final String USER_NAME_UNIQUE_MSG = "admin.restList.message.add.unique.userName";
	public static final String USER_NAME_EMPTY_MSG = "admin.restList.message.add.empty.userName";
	public static final String NICK_NAME_EMPTY_MSG = "admin.restList.message.add.empty.nickName";
	public static final String PASSWORD_EMPTY_MSG = "admin.restList.message.add.empty.password";
	public static final String CONFIRM_PASSWORD_EMPTY_MSG = "admin.restList.message.add.empty.confirmPassword";
	public static final String USER_NAME_HALF_MSG = "admin.restList.message.add.half.userName";
	public static final String USER_NAME_LENGTH_MSG = "admin.restList.message.add.length.userName";
	public static final String USER_NAME_FIRST_MSG = "admin.restList.message.add.first.userName";
	public static final String NICK_NAME_LENGTH_MSG = "admin.restList.message.add.length.nickName";
	public static final String PASSWORD_LENGTH_MSG = "admin.restList.message.add.length.password";
	public static final String CONFIRM_PASSWORD_SAME_MSG = "admin.restList.message.add.same.confirmPassword";
	public static final String LANG_ID_EMPTY_MSG = "admin.restList.message.add.empty.langId";

	private long restId;
	private String expirationDate;
	private RestraurantBean restBean;
	private UserBean userBeanPage;
	private String confirmPassword;
	private List<LanguageBean> langList;

	private String langId1;
	private String langId2;
	private String langId3;
	private String langId4;
	private String langId5;

	@Autowired
	private IAdminRestService adminRestService;

	public String init() {
		this.initInputPage();
		return SUCCESS;
	}

	public String save() {
		if (!this.checkParams()) {
			this.initInputPage();
			return INPUT;
		}

		this.restBean.setCreateUserId(super.getUserInfoLogin().getUserId());
		this.restBean.setUpdateUserId(super.getUserInfoLogin().getUserId());
		this.restBean.setLangId1(StringUtil.isNumeric(this.langId1) ? Long.parseLong(this.langId1) : 0);
		this.restBean.setLangId2(StringUtil.isNumeric(this.langId2) ? Long.parseLong(this.langId2) : 0);
		this.restBean.setLangId3(StringUtil.isNumeric(this.langId3) ? Long.parseLong(this.langId3) : 0);
		this.restBean.setLangId4(StringUtil.isNumeric(this.langId4) ? Long.parseLong(this.langId4) : 0);
		this.restBean.setLangId5(StringUtil.isNumeric(this.langId5) ? Long.parseLong(this.langId5) : 0);

		this.userBeanPage.setUserTypeId(Constants.USER_TYPE_MANAGER);
		this.userBeanPage.setPassword(MD5Util.encodeMD5(this.userBeanPage.getPassword()));
		this.userBeanPage.setCreateUserId(super.getUserInfoLogin().getUserId());
		this.userBeanPage.setUpdateUserId(super.getUserInfoLogin().getUserId());
		// this.userBeanPage.setDefLangId(Constants.DEF_LANG_ID);

		// 插入数据
		try {
			Map<String, Boolean> msgMap = this.adminRestService.insertRestWithoutId(this.restBean, this.userBeanPage);

			if (msgMap.isEmpty()) {
				this.addActionMessage(super.getText(ADD_SUCCESS_MSG));
				return SUCCESS;
			} else {
				if (msgMap.containsKey("msg.userName")) {
					this.addFieldError("msg.userName", super.getText(USER_NAME_UNIQUE_MSG));
				}
				if (msgMap.containsKey("msg.serverName")) {
					this.addFieldError("msg.serverName", super.getText(SERVER_NAME_UNIQUE_MSG));
				}

				this.initInputPage();
				return INPUT;
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.initInputPage();
			return INPUT;
		}
	}

	private boolean checkParams() {
		// 餐厅名验证
		if (StringUtil.isEmpty(this.restBean.getRestName())) {
			this.addFieldError("msg.restName", super.getText(REST_NAME_EMPTY_MSG));
		}

		// 域名验证
		if (StringUtil.isEmpty(this.restBean.getServerName())) {
			this.addFieldError("msg.serverName", super.getText(SERVER_NAME_EMPTY_MSG));
		}

		// 过期时间验证
		if (StringUtil.isEmpty(this.expirationDate)) {
			this.addFieldError("msg.expirationDate", super.getText(EXP_DATE_EMPTY_MSG));
		}
		
		// 设置过期日期
		try {
			this.restBean.setExpirationDate(StringUtil.parseDate(this.expirationDate, ErProperties.getProperty(Constants.PROPERTY_COMMON_DATETIME_FORMAT)));
		} catch (ParseException e) {
			this.addFieldError("msg.expirationDate", super.getText(EXP_DATE_EXPR_MSG));
		}

		// 用户名验证
		if (StringUtil.isEmpty(this.userBeanPage.getUserName())) {
			this.addFieldError("msg.userName", super.getText(USER_NAME_EMPTY_MSG));
		} else if (!StringUtil.isHalf(this.userBeanPage.getUserName())) {
			this.addFieldError("msg.userName", super.getText(USER_NAME_HALF_MSG));
		} else if (!StringUtil.checkLength(this.userBeanPage.getUserName(), 4, 32)) {
			this.addFieldError("msg.userName", super.getText(USER_NAME_LENGTH_MSG));
		} else if (!CharUtil.isLetter(this.userBeanPage.getUserName().charAt(0))) {
			this.addFieldError("msg.userName", super.getText(USER_NAME_FIRST_MSG));
		}

		// 用户昵称验证
		if (StringUtil.isEmpty(this.userBeanPage.getNickName())) {
			this.addFieldError("msg.nickName", super.getText(NICK_NAME_EMPTY_MSG));
		}
		if (!StringUtil.isEmpty(this.userBeanPage.getNickName())) {
			if (!StringUtil.checkLength(this.userBeanPage.getNickName(), 0, 64)) {
				this.addFieldError("msg.nickName", super.getText(NICK_NAME_LENGTH_MSG));
			}
		}

		// 用户密码验证
		if (StringUtil.isEmpty(this.userBeanPage.getPassword())) {
			this.addFieldError("msg.password", super.getText(PASSWORD_EMPTY_MSG));
		} else if (!StringUtil.checkLength(this.userBeanPage.getPassword(), 6, 16)) {
			this.addFieldError("msg.password", super.getText(PASSWORD_LENGTH_MSG));
		}

		// 用户确认密码验证
		if (StringUtil.isEmpty(this.getExpirationDate())) {
			this.addFieldError("msg.confirmPassword", super.getText(CONFIRM_PASSWORD_EMPTY_MSG));
		} else if (!StringUtil.isEqual(this.confirmPassword, this.userBeanPage.getPassword())) {
			this.addFieldError("msg.confirmPassword", super.getText(CONFIRM_PASSWORD_SAME_MSG));
		}

		// 餐厅语言不能一个都不选
		if (!StringUtil.isNumeric(this.langId1) && !StringUtil.isNumeric(this.langId2) && !StringUtil.isNumeric(this.langId3) && !StringUtil.isNumeric(this.langId4)
				&& !StringUtil.isNumeric(this.langId5)) {
			this.addFieldError("msg.langId", super.getText(LANG_ID_EMPTY_MSG));
		}

		// 错误信息数量
		int errCount = this.getActionErrors().size() + this.getFieldErrors().size();

		return errCount == 0;
	}

	private void initInputPage() {
		// 初始化语言
		this.langList = ErLanguages.getLangList();
	}

	public long getRestId() {
		return restId;
	}

	public void setRestId(long restId) {
		this.restId = restId;
	}

	public RestraurantBean getRestBean() {
		return restBean;
	}

	public void setRestBean(RestraurantBean restBean) {
		this.restBean = restBean;
	}

	public String getExpirationDate() {
		return expirationDate;
	}

	public void setExpirationDate(String expirationDate) {
		this.expirationDate = expirationDate;
	}

	public UserBean getUserBeanPage() {
		return userBeanPage;
	}

	public void setUserBeanPage(UserBean userBeanPage) {
		this.userBeanPage = userBeanPage;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public List<LanguageBean> getLangList() {
		return langList;
	}

	public void setLangList(List<LanguageBean> langList) {
		this.langList = langList;
	}

	public String getLangId1() {
		return langId1;
	}

	public void setLangId1(String langId1) {
		this.langId1 = langId1;
	}

	public String getLangId2() {
		return langId2;
	}

	public void setLangId2(String langId2) {
		this.langId2 = langId2;
	}

	public String getLangId3() {
		return langId3;
	}

	public void setLangId3(String langId3) {
		this.langId3 = langId3;
	}

	public String getLangId4() {
		return langId4;
	}

	public void setLangId4(String langId4) {
		this.langId4 = langId4;
	}

	public String getLangId5() {
		return langId5;
	}

	public void setLangId5(String langId5) {
		this.langId5 = langId5;
	}

}
